﻿$(function () {
    var vm = new Vue({
        el: "#app",
        data: {
            // 统计数据
            statistics: {
                projectSum: 0,
                totalCalls: 0,
                todaySum: 0,
                browseSum: 0,
            },
            dataList: [], // 数据列表
            cur: 1, // 当前页
            allNum: 1, // 总页数
            pageSize: 5, // 每页条数\
            userName: '',
            showNewFolderModal: false,
            projectName: '',
            projectDescription: ''
        },
        created: function () {
        },
        mounted: function () {
            this.userName = Hu.Cookies.get('HuUserName') || ''
            if (this.userName === '') {
                window.location.href = './index.html'
            } else {
                this.getHotKey() // 获取热词
                this.getQueryPage()
            }
        },
        computed: {
            // 分页索引
            indexs: function () {
                var _this = this;
                var left = 1; //默认起始值是1
                var right = _this.allNum; //终止值是全部
                var arr = [];
                if (this.allNum >= 5) {
                    if (this.cur > 3 && this.cur < this.allNum - 2) {
                        left = this.cur - 2
                        right = this.cur + 2
                    } else {
                        if (this.cur <= 3) {
                            left = 1
                            right = 5
                        } else {
                            right = this.allNum
                            left = this.allNum - 4
                        }
                    }
                }
                while (left <= right) {
                    arr.push(left)
                    left++
                }
                return arr
            }
        },
        methods: {
            getHotKey: function () {
                const _this = this;
                $.ajax({
                    url: "/ProjectApi/Indexdata",
                    // url: "http://39.106.120.139/ProjectApi/Indexdata",
                    data: {},
                    type: "get",
                    cache: false,
                    dataType: "json",
                    success: function (data) {
                        if (data.data.hotkeys) {
                            _this.statistics = data.data.statistics;
                        }
                    },
                    error: function (e) {
                        // view("异常！");
                        console.log('error', e);
                        // alert("异常！");
                    },
                });
            },
            getDataList: function (index) {
                const _this = this;
                $.ajax({
                    url: "/ProjectApi/GetFolderListByPage",
                    data: {
                        pageIndex: index || 1,
                        userName: this.userName
                    },
                    type: "get",
                    cache: false,
                    dataType: "json",
                    success: function (data) {
                        console.log(data)
                        for (let i = 0; i < data.data.length; i++) {
                            data.data[i].Create_Time = data.data[i].Create_Time || '-'
                            data.data[i].Modify_Time = data.data[i].Modify_Time || '-'
                        }
                        _this.dataList = data.data
                        _this.allNum = data.totalFolders / _this.pageSize + (data.totalFolders % _this.pageSize ? 1 : 0)
                    },
                    error: function (e) {
                        // view("异常！");
                        console.log('error', e);
                        // alert("异常！");
                    },
                });
            },
            handleNewFolderClick() {
                $('#newFolderModal').modal('show')
            },
            handleNewFolderModalClose() {
                $('#newFolderModal').modal('hide')
                this.projectName = ''
                this.projectDescription = ''

            },
            handleNewFolderModalConfirm() {
                $('#newFolderModal').modal('hide')
                const _this = this;
                $.ajax({
                    url: "/ProjectApi/CreateFolder",
                    data: {
                        name: this.projectName,
                        description: this.projectDescription,
                        userName: this.userName
                    },
                    type: "post",
                    cache: false,
                    dataType: "json",
                    success: function (data) {
                        _this.projectName = ''
                        _this.projectDescription = ''
                        _this.getDataList()
                    },
                    error: function (e) {
                        console.log('error', e)
                    },
                })
            },
            pageClick(index) {
                if (index) {
                    this.cur = index
                }
                let state = {page: this.curr}
                history.replaceState(state, '', 'folders.html?page=' + this.cur)
                this.getDataList(this.cur)
            },
            getQueryPage() {
                let subString = window.location.search.substring(1)
                let list = subString.split('&')
                let query = {}
                for (let i = 0; i < list.length; i++) {
                    let values = list[i].split('=')
                    query[values[0]] = values[1]
                }
                this.cur = query.page || 1
                this.getDataList(this.cur)
            }
        },
    });
});